package xxl.robotlegs.extensions.contextview 
{
	import xxl.robotlegs.extensions.contextview.impl.ContextView;
	import xxl.robotlegs.extensions.matching.impl.InstanceOfMatcher;
	import xxl.robotlegs.framework.api.IContext;
	import xxl.robotlegs.framework.api.IExtension;
	import xxl.robotlegs.framework.api.IInjector;
	import xxl.robotlegs.framework.api.ILogger;
	
	/**
	 * ...
	 * @author XXL
	 */
	public class ContextViewExtension implements IExtension 
	{
		private var _injector:IInjector;
		private var _logger:ILogger;
		
		public function ContextViewExtension() 
		{
			
		}
		
		public function toString():String
		{
			return "ContextViewExtension（简介：通过Inject可以获得本上下文的ContextView）";
		}
		
		private function beforeInitializing():void
		{
			if (!_injector.hasDirectMapping(ContextView))
			{
				_logger.error("必须有一个ContextView");
			}
		}
		
		private function handleContextView(contextView:ContextView):void
		{
			if (_injector.hasDirectMapping(ContextView))
			{
				_logger.warn("已经有一个ContextView了，忽略当前这个 {0}", [contextView.view]);
			}
			else
			{
				_logger.debug("注册了一个ContextView {0}", [contextView.view]);
				_injector.map(ContextView).toValue(contextView);
			}
		}
		
		/* INTERFACE xxl.robotlegs.framework.api.IExtension */
		
		public function extend(context:IContext):void 
		{
			_injector = context.injector;
			_logger = context.getLogger(this);
			context.beforeInitializing(beforeInitializing);
			context.addConfigHandler(new InstanceOfMatcher(ContextView), handleContextView);
		}
		
	}

}